package com.topone.controller;

import com.topone.Converter.ConvenientInfoForm2ConvenientInfo;
import com.topone.Enum.ConvenientInfoEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Exception.FanZoneServiceException;
import com.topone.Form.ConvenientInfoForm;
import com.topone.Utils.KeyUntil;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.ConvenientInfo;
import com.topone.service.ConvenientInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Administrator on 2018/1/1,0001.
 */
@CrossOrigin
@RestController
@RequestMapping("/seller/convenientInfo")
@Slf4j
public class SellerConvenientInfoController {
    @Autowired
    private ConvenientInfoService service;

    @GetMapping("/list")
    public ResultVO list(@RequestParam("status") Integer status) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<ConvenientInfo> convenientInfoList = service
                    .findUpAll(status);
            map.put("convenientInfoList", convenientInfoList);
            log.info("查询所有便民信息成功:{}", convenientInfoList);
        } catch (Exception e) {
            log.info("查询所有便民信息失败:{}", status);
            return ResultVOUntil.error(1, e.getMessage());
        }
        return ResultVOUntil.success(map);
    }


    @GetMapping("/check")
    public ResultVO check(@RequestParam("convenientInfoId") String convenientInfoId,
                          @RequestParam("status") Integer status,
                          @RequestParam("updator") String updator) {
        if (convenientInfoId != null && status != null) {
            try {
                ConvenientInfo convenientInfo = service.findOne(convenientInfoId);
                if ((status == ConvenientInfoEnum.ON.getCode()
                        && convenientInfo.getConvenientStatus() == ConvenientInfoEnum.CHECK.getCode())
                        || (status == ConvenientInfoEnum.OFF.getCode()
                        && convenientInfo.getConvenientStatus() == ConvenientInfoEnum.ON.getCode())) {
                    if (Objects.nonNull(convenientInfo)) {
                        convenientInfo.setConvenientStatus(status);
                        convenientInfo.setUpdator(updator);
                        service.save(convenientInfo);
                        return ResultVOUntil.success("审批通过");
                    } else {
                        return ResultVOUntil.error(1, "数据不存在:" + convenientInfoId);
                    }
                } else {
                    return ResultVOUntil.error(1, "便民信息状态不正确,无法保存操作");
                }
            } catch (FanZoneException e) {
                log.debug("审批便民信息失败:{}", e.fillInStackTrace());
                throw new FanZoneServiceException("审批便民信息失败", e.fillInStackTrace());
            }
        } else {
            return ResultVOUntil.error(1, "参数不能为空");
        }

    }

    @PostMapping(value = "/save")
    public ResultVO save(@RequestBody ConvenientInfoForm convenientInfoForm) {
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(convenientInfoForm)) {
            return ResultVOUntil.error(1, "参数为空 convenientInfoForm=" + convenientInfoForm);
        }
        ConvenientInfo convenientInfo = ConvenientInfoForm2ConvenientInfo
                .convert(convenientInfoForm);
        log.debug("传入数据:{}", convenientInfo);

        //生成id
        convenientInfo.setConvenientId(KeyUntil.getUniqueKey());
        //提交订单 默认为待审核
        convenientInfo.setConvenientStatus(ConvenientInfoEnum.ON.getCode());
        try {
            service.save(convenientInfo);
        } catch (FanZoneException e) {
            log.error("【便民信息上传】申请保存失败!", e.getMessage());
            return ResultVOUntil.error(1, "【便民信息上传】申请保存失败!");
        }
        map.put("success", "提交成功");
        return ResultVOUntil.success(map);
    }
}
